package com.gzy.studentsociety.web;

import com.gzy.studentsociety.dao.StudentDao;
import com.gzy.studentsociety.dao.StudentDaoImpl;
import com.gzy.studentsociety.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;

@WebServlet(name = "student", urlPatterns = "/student/*")
public class StudentServlet extends RouteServlet {

    private StudentDao studentDao = new StudentDaoImpl();
    @Override
    protected Class getMyClass() {
        return this.getClass();
    }
    public String login(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        Student stu = null;
        try {
            stu = studentDao.login(account, password);
        } catch (SQLException throwables) {
            handleException(throwables, req, res);
        }
        if (stu != null) {
            HttpSession session = req.getSession();
            session.setAttribute("student", stu.getStudentNumber());
            res.sendRedirect("/studentSociety/society/mainIndex");
        } else {
            req.setAttribute("message", "账号或密码错误，请重新输入。");
            req.getRequestDispatcher("/index.jsp").forward(req, res);
        }
        return "direct";
    }

    // 注册方法
    public String register(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // 获取注册信息
        String studentName = req.getParameter("studentName");
        String studentNumber = req.getParameter("studentNumber");
        String gender = req.getParameter("gender");
        String password = req.getParameter("password");

        // 校验输入
        if (studentName == null || studentName.isEmpty() || studentNumber == null || studentNumber.isEmpty() ||
                password == null || password.isEmpty()) {
            req.setAttribute("message", "所有字段必须填写！");
            req.getRequestDispatcher("/register.jsp").forward(req, res);
            return null;
        }

        // 创建学生对象
        Student student = new Student();
        student.setStudentNumber(studentNumber);
        student.setStudentName(studentName);
        student.setGender(gender);
//        student.setBirthday("2002-01-01");  // 设置默认生日
        student.setPassword(password);
        student.setGraduated(1); // 默认未毕业
        student.setAvatarPath("img/user.png"); // 默认头像路径

        try {
            // 调用 StudentDao 的 register 方法
            boolean success = studentDao.register(student);
            if (success) {
                req.setAttribute("message", "注册成功，请登录！");
                req.getRequestDispatcher("/index.jsp").forward(req, res);
            } else {
                req.setAttribute("message", "注册失败，请稍后再试。");
                req.getRequestDispatcher("/register.jsp").forward(req, res);
            }
        } catch (SQLException e) {
            e.printStackTrace(); // 记录异常信息
            handleException(e, req, res);  // 异常处理
        }
        return null;  // 确保页面跳转
    }

}